public class MySingleList {
    static class ListNode{
        public int val;//节点的值域
        public ListNode next;//下一个节点的地址

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;//表示当前链表的头结点
    public void creatNode(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        //一般建议在插入的时候先绑定后面的节点信息
        node.next = head;
        this.head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode cur = head;
        ListNode node = new ListNode(data);
        if(cur == null){
            //如果链表中没有元素，那么让头结点指向node
            head = node;
            return;
        }
        //当cur.next == null的时候，那么该节点就是尾巴节点
        //当cur == null证明该链表已经被遍历完了
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index < 0 || index > size()){
            throw new IndexErrorException("下标不合法");
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        else if (index == size()){
            addLast(data);
            return;
        }
        //1、定义cur走index-1步
        //2、进行插入
        ListNode node = new ListNode(data);
        ListNode cur = head;
        int count = 0;
        while(count != index-1){
            cur = cur.next;
            count++;
        }
        node.next = cur.next;
        cur.next = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            return;
        }
        //单独删除头结点
        if(head.val == key){
            head = head.next;
            return;
        }
        //找到删除节点的前一个结点
        ListNode cur = head;
        while (cur != null)
        {
            if(cur.next.val == key){//此时cur指向的节点就是要删除节点的前一个节点
                    if(cur.next.next==null){
                        cur.next = null;
                        break;
                    }
                    //cur.next = cur.next.next
                cur.next = cur.next.next;
                break;
            }
            cur = cur.next;
        }
        if(cur==null){
            System.out.println("没有你要删除的数字");
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        //单独删除头结点
        if(head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = head;
        while (cur != null)
        {
            if(cur.next.val == key){
                if(cur.next.next==null){
                    cur.next = null;
                    break;
                }
                cur.next = cur.next.next;
            }
            cur = cur.next;
        }
        if(cur==null){
            System.out.println("没有你要删除的数字");
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void clear() {
        ListNode cur = head;
        while (cur != null){
            cur = null;
            cur = cur.next;
        }
    }
    public void display() {
        ListNode cur = head;
        while (cur!= null){//如果cur==null证明把链表遍历完成了
            System.out.print(cur.val + " ");
            cur = cur.next;//cur每次都在向后走一步
}
        System.out.println();
    }
}
